package com.yang.controller.admin;

import com.yang.bean.MarketRole;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.common.CommonData;
import com.yang.bean.dhr.MarketRoleOptionsVo;
import com.yang.bean.dhr.MarketRoleVo;
import com.yang.bean.yvo.qunxianvo.PerPostBO;
import com.yang.bean.yvo.qunxianvo.PermissionVO;
import com.yang.service.admin.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author dinghongrui
 * @create 2022/09/07 16:08
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;

    @RequestMapping("list")
    public BaseRespVo queryAllRole(Integer page, Integer limit, String sort, String order,String name) {
        CommonData<MarketRole> commonData = adminRoleService.selectAllRole(page, limit, sort, order, name);
        BaseRespVo baseRespVo = BaseRespVo.ok(commonData);
        return baseRespVo;
    }

    @RequestMapping("options")
    public BaseRespVo<CommonData<MarketRoleOptionsVo>> queryAllRoleOptions() {
        CommonData<MarketRoleOptionsVo> commonData = adminRoleService.selectAllRoleOptions();
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping("create")
    public BaseRespVo<MarketRoleVo> createRole(@RequestBody MarketRole marketRole) {
        MarketRoleVo marketRoleVo = adminRoleService.createRole(marketRole);
        return BaseRespVo.ok(marketRoleVo);
    }

    @RequestMapping("update")
    public BaseRespVo<MarketRole> updateRole(@RequestBody MarketRole marketRole) {
        adminRoleService.updateRole(marketRole);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo<MarketRole> deleteRole(@RequestBody MarketRole marketRole) {
        adminRoleService.deleteRole(marketRole);
        return BaseRespVo.ok(null);
    }


    /**
    * @description: 授权的查询
    [roleId]
    * @return: com.yang.bean.common.BaseRespVo
    * @author: 杨庆
    * @time: 2022.9.12 10:53
     */
    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId) {
        PermissionVO permissionVO = adminRoleService.selectPermissions(roleId);
        return BaseRespVo.ok(permissionVO);
    }

    @PostMapping("permissions")
    public BaseRespVo permissionsPost(@RequestBody PerPostBO perPostBO) {
        if (perPostBO.getRoleId() == 1) {
            return BaseRespVo.error("超级管理员不支持修改！");
        }
        //向权限表里边提交数据
        adminRoleService.updatePermissions(perPostBO);
        return BaseRespVo.ok();
    }



}
